package com.guicelcc.guicedemo.poi;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author ycp
 * @version V1.0
 * @Description
 * @date 2018/10/21 10:38 PM
 */
public class DocWriter {
    
    public static void writeToDoc(String outPutPath, List<TestData> data, String title) {
    
        XWPFDocument document = new XWPFDocument();
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText(title);
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
        createOtherLine(document);
        packageTiankong(data, document);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(outPutPath);
            document.write(fileOutputStream);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 组装选择题
     * @param data
     * @param document
     */
    private static void packageSelect(List<TestData> data, XWPFDocument document) {
        data.stream().forEach(c -> {
            packageQuestionAndAns(c, document);
        });
        packageRightAns(data, document);
    }
    
    /**
     * 组装填空题
     * @param data
     * @param document
     */
    private static void packageTiankong(List<TestData> data, XWPFDocument document) {
        data.stream().forEach(c -> {
            XWPFParagraph firstParagraph = document.createParagraph();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(c.getId());
            stringBuilder.append("、");
            stringBuilder.append(c.getQuestion());
            XWPFRun run = firstParagraph.createRun();
            run.setText(stringBuilder.toString());
            run.setColor("000000");
            run.setFontSize(10);
            XWPFParagraph ansParagraph = document.createParagraph();
            XWPFRun run2 = ansParagraph.createRun();
            run2.setText("答案：" + c.getRightAns());
            run2.setColor("000000");
            run2.setFontSize(10);
            createOtherLine(document);
        });
    }
    
    /**
     * 组装判断题
     * @param data
     * @param document
     */
    private static void packagePanDuan(List<TestData> data, XWPFDocument document) {
        data.stream().forEach(c -> {
            packageQuestion(c, document);
            createOtherLine(document);
        });
        packageRightAnsForPuan(data, document);
    }
    private static void packageQuestionAndAns(TestData data, XWPFDocument document) {
        packageQuestion(data, document);
        packageSingleAns(data, document);
        createOtherLine(document);
    }
    private static void packageQuestion(TestData data, XWPFDocument document) {
        XWPFParagraph firstParagraph = document.createParagraph();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(data.getId());
        stringBuilder.append("、");
        stringBuilder.append(data.getQuestion());
        XWPFRun run = firstParagraph.createRun();
        run.setText(stringBuilder.toString());
        run.setColor("000000");
        run.setFontSize(10);
    }
    private static void packageRightAnsForPuan(List<TestData> datas, XWPFDocument document) {
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        
        StringBuilder stringBuilder = new StringBuilder();
        datas.stream().forEach(c -> {
            stringBuilder.append(c.getId() + ":" + (c.getRightAns().equals("A") ? "对" : "错") + "  ");
        });
        run.setText(stringBuilder.toString());
        run.setColor("000000");
        run.setFontSize(10);
    }
    private static void packageRightAns(List<TestData> datas, XWPFDocument document) {
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        
        StringBuilder stringBuilder = new StringBuilder();
        datas.stream().forEach(c -> {
            stringBuilder.append(c.getId() + ":" + c.getRightAns() + "  ");
        });
        run.setText(stringBuilder.toString());
        run.setColor("000000");
        run.setFontSize(10);
    }
    private static void packageSingleAns(TestData data, XWPFDocument document) {
        if(data.getAns1().length() + data.getAns2().length() + data.getAns3().length() + data.getAns4().length() > 20) {
            XWPFParagraph firstParagraph = document.createParagraph();
            XWPFRun run = firstParagraph.createRun();
            run.setText("A、" + data.getAns1());
            run.setColor("000000");
            run.setFontSize(10);
    
            XWPFParagraph firstParagraph1 = document.createParagraph();
            XWPFRun run1 = firstParagraph1.createRun();
            run1.setText("B、" + data.getAns2());
            run1.setColor("000000");
            run1.setFontSize(10);
    
            XWPFParagraph firstParagraph2 = document.createParagraph();
            XWPFRun run2 = firstParagraph2.createRun();
            run2.setText("C、" + data.getAns3());
            run2.setColor("000000");
            run2.setFontSize(10);
    
            XWPFParagraph firstParagraph3 = document.createParagraph();
            XWPFRun run3 = firstParagraph3.createRun();
            run3.setText("D、" + data.getAns4());
            run3.setColor("000000");
            run3.setFontSize(10);
        } else {
            XWPFParagraph firstParagraph = document.createParagraph();
            firstParagraph.setAlignment(ParagraphAlignment.MEDIUM_KASHIDA);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("A、");
            stringBuilder.append(data.getAns1());
            stringBuilder.append("   ");
            stringBuilder.append("B、");
            stringBuilder.append(data.getAns2());
            stringBuilder.append("   ");
            stringBuilder.append("C、");
            stringBuilder.append(data.getAns3());
            stringBuilder.append("   ");
            stringBuilder.append("D、");
            stringBuilder.append(data.getAns4());
    
            XWPFRun run = firstParagraph.createRun();
            run.setText(stringBuilder.toString());
            run.setColor("000000");
            run.setFontSize(10);
        }
    }
    
    private static void createOtherLine(XWPFDocument document) {
        XWPFParagraph firstParagraph = document.createParagraph();
    
        XWPFRun run = firstParagraph.createRun();
        run.setText("\r");
    }
}
